package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.service.IStrategyCatalogService;
import cn.wolfcode.trip.base.service.IStrategyCommentService;
import cn.wolfcode.trip.base.service.IStrategyDetailService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 控制器
 */
@RestController
@RequestMapping("strategies")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyCatalogService strategyCatalogService;

    @Autowired
    private IStrategyDetailService strategyDetailService;
    @Autowired
    private IStrategyCommentService strategyCommentService;

    /**
     * 获取攻略资源
     * @param qo
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public PageInfo query(StrategyQueryObject qo){

        return strategyService.query(qo);

    }



    /**
     * 根据攻略id查询大攻略信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    //不是对象参数,是单个参数,要加PathVariable
    public Strategy getById(@PathVariable Long id){

        return strategyService.getById(id);

    }

    /**
     * 根据攻略id查询攻略分类信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/catalogs",method = RequestMethod.GET)
    //不是对象参数,是单个参数,要加PathVariable
    public List<StrategyCatalog> listCatalogByStrategyId(@PathVariable Long id){

        return strategyCatalogService.listByStrategyId(id);

    }

    /**
     * 根据攻略文章id查询文章信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/details/{id}",method = RequestMethod.GET)
    //不是对象参数,是单个参数,要加PathVariable
    public StrategyDetail getByDetailId(@PathVariable Long id){
        return strategyDetailService.getByDetailId(id);
    }

    //不是对象参数,是单个参数,要加PathVariable
    /**
     * 新增评论
     * @return
     */
    @RequestMapping(value = "/{strategyId}/{userId}/comments",method = RequestMethod.POST)
    public JSONResult saveStrategyComments(@PathVariable Long strategyId,@PathVariable Long userId,Integer starNum,String content,String imgUrls){
        JSONResult result = new JSONResult();
        StrategyComment comment = new StrategyComment();
        User user = new User();
        user.setId(userId);
        Strategy strategy = new Strategy();
        strategy.setId(strategyId);
        comment.setStarNum(starNum);
        comment.setContent(content);
        comment.setImgUrls(imgUrls);
        comment.setStrategy(strategy);
        comment.setUser(user);
        try {
            //strategyCommentService.saveStrategyComments(comment);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
}
